﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for GDELIVERY.xaml
    /// </summary>
    public partial class GDELIVERY : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        GDELIVERYData m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataRow dwABPOS;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        GDELIVERYReport m_rpt;
        // string OrderNumber="";
        
        #endregion
        public GDELIVERY()
        {
            InitializeComponent();
            this.Name = "坯布发货单";
        }
        public override void SetUp (Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           
            
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new GDELIVERYData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }
        private void InitDataBinding()
        {
            if (!setUI.Tables.Contains("WBBEW"))
            {
                this.setUI.Tables.Add("WBBEW");
            }

            if (!setUI.Tables.Contains("WBBST"))
            {
                this.setUI.Tables.Add("WBBST");
            }


            orlReadCommand = OracleDB.CreateCommand();

            #region "WBBEW"
            orlReadCommand.CommandText = " SELECT  '' AS FACTORY,WBBART,WBBVAR,WBBFARBE,WBBDPART,WBBLP, "
                + " SUM(WBBISTKLG) AS MTR,SUM(1) AS PCNUM,'' AS WBEWBUCHUHR  "
                + " FROM WBBST WHERE 1=2 "
                + " GROUP BY WBBART,WBBVAR,WBBFARBE,WBBDPART,WBBLP ";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "WBBEW";
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion
          
            #region "WBBST"
            orlReadCommand.CommandText = " SELECT '' AS FACTORY,WBBART,WBBVAR,WBBFARBE,WBBDPART,WBBLP, "
                + " WBBISTKLG,WBBSTKNR,'' AS WBEWBUCHUHR,WBBERLKZ  FROM WBBST WHERE 1=2 ";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "WBBST";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion


            setUI.AcceptChanges();
            this.WBBEWDataGrid.DataContext = setUI;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            
  
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DataTable dtTemp in setUI.Tables)
            {
                if (dtTemp.TableName != "WBBEW")
                {
                    dtTemp.Clear();
                }
            }
            setUI.AcceptChanges();
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtFACTORY.Text = dwABPOS["FACTORY"].ToString();
                this.txtArticle.Text = dwABPOS["WBBART"].ToString().Trim()+"-"
                    + dwABPOS["WBBFARBE"].ToString().Trim()+"-"
                    + dwABPOS["WBBVAR"].ToString().Trim();
                this.txtBATCHNO.Text = dwABPOS["WBBDPART"].ToString().Trim();
                
                //Tracer.Speak("销售单："+txtVKPAUFLFD.Text);
                ReloadWBBST(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }

        private void ReloadWBBST(DataRow dwABPOS)
        {
            dwWBBST = setUI.Tables["WBBST"].Clone().Rows.Add();
            dwWBBST["WBBLP"] = dwABPOS["WBBLP"];
            dwWBBST["WBBART"] = dwABPOS["WBBART"];
            dwWBBST["WBBVAR"] = dwABPOS["WBBVAR"];
            dwWBBST["WBBFARBE"] = dwABPOS["WBBFARBE"];
            dwWBBST["WBBDPART"] = dwABPOS["WBBDPART"];
            dwWBBST["WBEWBUCHUHR"] = dwABPOS["WBEWBUCHUHR"];
            string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
            if (ERLKZ == "True")
            {
                dwWBBST["WBBERLKZ"] = "J";
            }
            else
            {
                dwWBBST["WBBERLKZ"] = "N";
            }
            DataSet setResult = m_Data.GetWBBST(dwWBBST);
           
            setUI.Tables["WBBST"].Clear();
            setUI.Merge(setResult);
            listWBBST.DataContext = setUI;
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {

              
                    dicParamater["ARTICLE"] = this.txtArticle.Text;
                    dicParamater["FACTORY"] = this.txtFACTORY.Text;
                    dicParamater["WBBDPART"] = this.txtBATCHNO.Text;
                    m_DataX = new GDELIVERYData(dicParamater);
                    m_DataX.OracleDB = this.OracleDB;
                    m_DataX.proxy = this.proxy;
                    m_rpt = new GDELIVERYReport(dicParamater);
                    m_rpt.dsSource = setUI;
                    m_rpt.PrintType = PrintType;
                    m_rpt.exAgent = m_DataX.exAgent;
                    m_rpt.GetGDELIVERYReport("EXCEL");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));
                    InitDataBinding();
                    setResult = m_Data.LoadWBBEW();
                    setUI.Clear();
                    setUI.Merge(setResult);
                    this.WBBEWDataGrid.DataContext = setUI;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

       
    }
}
